package com.easepal802s.project.ui.activity;

import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;

import com.clj.fastble.data.BleDevice;
import com.easepal802s.project.R;
import com.easepal802s.project.ble.BleController;
import com.easepal802s.project.observer.BleDataObserver;
import com.easepal802s.project.observer.CloseObserver;
import com.easepal802s.project.ui.presenter.ScanShoulderPresenter;
import com.easepal802s.project.ui.iview.IScanShoulderView;
import com.easepal802s.project.utils.ActivityUtils;
import com.ogawa.base.utils.CommonUtil;

import static com.easepal802s.project.ble.BleController.CONNECT_BREAK;

/**
 * Created by FHB on 2018/3/27.
 */

public class ScanShoulderActivity extends BaseMvpTestActivity<ScanShoulderPresenter, ScanShoulderActivity> implements CloseObserver.OnCloseListerner,IScanShoulderView , BleDataObserver.OnBleListerner{
    private ImageView mIvScan;
    private AnimationDrawable mAnimation;
    private static onCloseScan mAcation;
    private ImageView mIvBack;

    @Override
    public ScanShoulderPresenter initPresenter() {
        return new ScanShoulderPresenter(this);
    }

    @Override
    protected void initCreateView(Bundle savedInstanceState) {
        setContentView(R.layout.activity_scan_shoulder);
        BleDataObserver.getInst().registerBleDataObserver(this);
        CloseObserver.getInst().registerObserver(this);
        mIvScan = findViewById(R.id.at_scan);
        mIvBack= findViewById(R.id.iv_back);
        mAnimation=(AnimationDrawable)mIvScan.getBackground();
        mAnimation.start();
        mIvBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ScanShoulderActivity.this.finish();
            }
        });

    }

    @Override
    public ScanShoulderActivity initActivity() {
        return this;
    }

    @Override
    public void getData(String data) {

    }



    @Override
    protected void onDestroy() {
        super.onDestroy();
        mAnimation.stop();
        mAcation = null;
        BleDataObserver.getInst().removeBleDataObserver(this);
        CloseObserver.getInst().removeObserver(this);
        mIvScan.clearAnimation();

    }

    @Override
    public void startToActivity() {
        Log.e("startToActivity","startToActivity");
        if(mPresenter.getIsCan()){
            ActivityUtils.startShouldersAdjustmentActivity(this);
            this.finish();
        }
        else {
            this.finish();
        }

    }



    public static void setScanAction(onCloseScan action){
        mAcation = action;
    }

    @Override
    public void onConnectState(int state) {

    }

    @Override
    public void onTargetDeviceFind(BleDevice bleDevice) {

    }

    @Override
    public void onStarScan() {

    }

    @Override
    public void onNoDeviceFind() {

    }

    @Override
    public void onGetData(String data) {
        mBasePresenter.getDatas(data);
    }

    @Override
    public void onPowerOff() {
        if(mAcation!=null){
            mAcation.scanClose();
        }
        if(BleController.getInst().getConnectState()==CONNECT_BREAK){
            ActivityUtils.startHomeActivity(this,"BREAK");
        }
        else {
            ActivityUtils.startHomeActivity(this,"none");
        }
        this.finish();
    }

    @Override
    public void onDataChange(String bage, int auto, int skill, int air,String name,String commond) {
//             if(auto==0){
//
//                       this.finish();
//                   }
    }

    @Override
    public void onShoulderAdjust(int data) {
        if(CommonUtil.isActivityTop(this.getClass(),this)&&data==1){
            ActivityUtils.startShouldersAdjustmentActivity(this);
        }
    }

    @Override
    public void onClose() {
        if(mAcation!=null){
            mAcation.scanClose();
        }
        this.finish();
    }

    public interface onCloseScan{
        void scanClose();

    }



    @Override
    public void onBackPressed() {
        if(mAcation!=null){
            mAcation.scanClose();
        }
//        handler.removeCallbacks(runnable);
        this.finish();
    }





}
